// pages/priceSystem/index.js
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: [0, 1, 2, 3, 4, 5],
    yearSelect: '', // 选择年份
    show: false, // 年份选择显隐
    yearColumns: [], // 年份集合
    onSelectType: 0, // 种类选择
    onSelectMonth: 1, // 月份选择
    onSelectWeek: 1, // 周选择
    productType: [], // 种类集合
    month: [{
      value: 1,
      label: '一月'
    }, {
      value: 2,
      label: '二月'
    }, {
      value: 3,
      label: '三月'
    }, {
      value: 4,
      label: '四月'
    }, {
      value: 5,
      label: '五月'
    }, {
      value: 6,
      label: '六月'
    }, {
      value: 7,
      label: '七月'
    }, {
      value: 8,
      label: '八月'
    }, {
      value: 9,
      label: '九月'
    }, {
      value: 10,
      label: '十月'
    }, {
      value: 11,
      label: '十一月'
    }, {
      value: 12,
      label: '十二月'
    }],
    week: [{
      value: 1,
      label: '第一周'
    }, {
      value: 2,
      label: '第二周'
    }, {
      value: 3,
      label: '第三周'
    }, {
      value: 4,
      label: '第四周'
    }],
    submitLoading: false,
    errText: '请输入正确价格，至多两位小数' // 错误提示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const date = new Date(); // 创建一个日期对象
    const years = []; // 创建一个空数组用于存储年份
    for (let i = 2000; i <= date.getFullYear(); i++) {
      years.push(i); // 将年份添加到数组中
    }
    this.setData({
      yearColumns: years,
      yearSelect: date.getFullYear()
    })
    this.getDetailPrice()
  },
  // /dev-api/wms/productPriceInfo/getPrice
  getDetailPrice() {
    const that = this
    wx.$req({
      url: '/wms/productPriceInfo/getPrice',
      data: {
        year: that.data.yearSelect,
        month: that.data.onSelectMonth,
        week: that.data.onSelectMonth == 9 ? that.data.onSelectWeek : that.data.onSelectMonth == 10 ? that.data.onSelectWeek : ''
      }
    }).then(res => {
      const data = res.data.prices.map(item => {
        return {
          type: item.type,
          typeName: that.changeName(item.type),
          price: item.price,
          isError: false
        }
      })
      that.setData({
        productType: data
      })
    })
  },
  changeName(e) {
    let text = ''
    switch (e) {
      case 0:
        text = '济薯26'
        break;
      case 1:
        text = '济薯25'
        break;
      case 2:
        text = '烟薯25'
        break;
      case 3:
        text = '其他高端产品'
        break;
      case 4:
        text = '泗薯1号'
        break;
      case 5:
        text = '紫薯1号'
        break;
    }
    return text
  },
  oninits() {
    console.log(11)
    this.setData({
      submitLoading: true
    })
  },
  onPriceChange(e) {
    const that = this
    var checked = "productType[" + e.currentTarget.dataset.index + "].isError"
    var checkedPrice = "productType[" + e.currentTarget.dataset.index + "].price"
    let reg = /^\d+(\.\d{1,2})?$/
    if (e.detail.value && !reg.test(e.detail.value)) {
      this.setData({
        [checked]: true,
      })
    } else {
      this.setData({
        [checked]: false,
        [checkedPrice]: e.detail.value
      })
      if (e.currentTarget.dataset.price) {
        wx.$req({
          url: "/wms/productPriceInfo",
          method: 'put',
          data: {
            price: e.currentTarget.dataset.price,
            type: e.currentTarget.dataset.type,
            year: that.data.yearSelect,
            month: that.data.onSelectMonth,
            week: that.data.onSelectMonth == 9 ? that.data.onSelectWeek : that.data.onSelectMonth == 10 ? that.data.onSelectWeek : ''
          }
        }).then(res => {
          Notify({
            type: 'success',
            message: '已保存'
          })
          that.getDetailPrice()
        })
      }

    }
    this.setData({
      submitLoading: false
    })
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  onType(e) {
    this.setData({
      onSelectType: e.currentTarget.dataset.type
    })
  },
  onMonth(e) {
    this.setData({
      onSelectMonth: e.currentTarget.dataset.type
    })
    this.getDetailPrice()
  },
  onWeek(e) {
    this.setData({
      onSelectWeek: e.currentTarget.dataset.type
    })
    this.getDetailPrice()
  },
  // 暂时作废
  submit() {
    console.log('提交', this.data.productType)
    let isTrue = true
    for (let key in this.data.productType) {
      if (this.data.productType[key].isError) {
        isTrue = false
      }
    }
    if (!this.data.submitLoading) {
      return
    }
    this.setData({
      submitLoading: false
    })
    console.log('提交2', isTrue)
    wx.$req({
      url: "/wms/productPriceInfo",
      method: 'put',
      data: {
        month: '',
        month: '',
        month: '',
        month: '',
      }
    }).then(res => {
      this.setData({
        submitLoading: true
      })
    }).catch(() => {
      this.setData({
        submitLoading: true
      })
    })
  },
  openYear() {
    this.setData({
      show: true
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  onConfirm(e) {
    console.log(e)
    this.setData({
      yearSelect: e.detail.value,
      show: false
    })
    this.getDetailPrice()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})